package com.megalith.controller;

import com.megalith.dto.ActivityRebackDTO;
import com.megalith.dto.TaskAssignDTO;
import com.megalith.service.CandidateTaskService;
import com.megalith.service.CommonTaskService;
import com.megalith.utils.RequestResponseUtil;
import com.megalith.vo.HistoricActivityInstanceEntityVO;
import com.megalith.vo.HistoricTaskInstanceVO;
import com.megalith.vo.Result;
import com.megalith.vo.TaskEntityVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 通用流程接口
 * @Author lfp
 * @Date 2022/7/16
 * @Version 1.0
 */
@RestController
@Api(tags = "通用流程&任务接口")
@RequestMapping("common")
class CommonTaskController {

    @Autowired
    private CandidateTaskService candidateTaskService;

    @Autowired
    private CommonTaskService commonTaskService;

    /**
    *@Description 查询指定流程当前办理节点
    *@Author lfp
    *@Date 2022/7/16
    *@param processInstanceId
    *@Return
    */
    @ApiOperation(value = "查询指定流程当前办理节点")
    @GetMapping("process/current/{processInstanceId}")
    public Result<TaskEntityVO> queryProcessCurrentTask(@PathVariable String processInstanceId){
        return Result.ok(commonTaskService.queryProcessCurrentTask(processInstanceId));
    }

    /**
     *@Description 查询所有个人待办任务（包含待认领）
     *@Author lfp
     *@Date 2022/7/16
     *@param userId
     *@Return
     */
    @ApiOperation(value = "查询所有个人待办任务（包含待认领）",notes = "个人待办或待认领任务，不包括组任务")
    @GetMapping("task/personal/list/{userId}")
    public Result<List<TaskEntityVO>> listPersonalAssignmentTask(@PathVariable String userId){
        return Result.ok(commonTaskService.listPersonalAssignmentTask(userId));
    }


    /**
    *@Description 根据个人Id，查询可以认领的任务列表（包括组任务）
    *@Author lfp
    *@Date 2022/7/16
    *@param userId
    *@Return
    */
    @ApiOperation(value = "根据个人Id，查询可以认领的任务列表",notes = "任务列表包括可领取的个人任务 和 组任务")
    @GetMapping("personal/list/{userId}")
    public Result<List<TaskEntityVO>> listPersonalCandidateTask(@PathVariable String userId){
        return Result.ok(commonTaskService.listPersonalCandidateTask(userId));
    }


    @ApiOperation(value = "根据流程实例编号，查询历史任务信息")
    @GetMapping("history/task/list/{processInstanceId}")
    public Result<List<HistoricTaskInstanceVO>> queryHistoricTaskInfoList(@PathVariable String processInstanceId){
        return Result.ok(commonTaskService.queryHistoricTaskInfoList(processInstanceId));
    }


    @ApiOperation(value = "根据流程实例编号，查询历史活动信息")
    @GetMapping("activity/history/list/{processInstanceId}")
    public Result<List<HistoricActivityInstanceEntityVO>> queryHistoricActivityInfoList(@PathVariable String processInstanceId){
        return Result.ok(commonTaskService.queryHistoricActivityInfoList(processInstanceId));
    }

    @ApiOperation(value = "任务交接（将任务指派给指定人）")
    @PostMapping("assign/task")
    public Result assignTaskToOthers(@Validated @RequestBody TaskAssignDTO taskAssignDTO, BindingResult result){
        if (result.hasErrors()) {
            return RequestResponseUtil.doBindingResultHandel(result);
        }else {
            return commonTaskService.assignTaskToOthers(taskAssignDTO);
        }
    }


    @ApiOperation(value = "流程回退")
    @PostMapping("actibity/reback")
    public Result processActivityReback(@Validated @RequestBody ActivityRebackDTO rebackDTO, BindingResult result){
        if (result.hasErrors()) {
            return RequestResponseUtil.doBindingResultHandel(result);
        }else {
            commonTaskService.processActivityReback(rebackDTO);
            return Result.ok();
        }
    }
}
